//
//  CommissionExController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/3/16.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "CommissionExController.h"
#import "CommissionExCell.h"
#import "CommissionExModel.h"

@interface CommissionExController ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *_progressHUD;
    NSUInteger refreshIndex;
    UIImageView *noDrawImage;
}

@property(nonatomic,strong)NSMutableArray *commissiomExArray;

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation CommissionExController

static NSString * const commissionExIdentifier =  @"commissionExCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self buildView];
    
}

- (void)buildView
{
    refreshIndex = 1;
    
    self.title = @"佣金记录";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT)];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 115;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    
    [self getCommissionListRequest:refreshIndex];
    
    //上拉刷新
    self.tableView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(loadCommissionExlistMoreData)];

}

- (void)loadCommissionExlistMoreData
{
    refreshIndex ++;
    [self getCommissionListRequest:refreshIndex];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark 请求
- (void)getCommissionListRequest:(NSInteger)index
{
    _progressHUD = [MBProgressHUD show:self.view andMessage:nil];
    [self.view addSubview:_progressHUD];
    
    if (index == 1) {
        [self.commissiomExArray removeAllObjects];
    }
    
    NSDictionary *params = @{@"username":USERNAME,@"page":[NSString stringWithFormat:@"%zd",index]};
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Expand/goodsList" completeBlock:^(id object) {
        
        if (JudgeBackDataCode) {
            
            NSArray *dataArr = [object objectForKey:@"data"];
            for (NSDictionary *dict in dataArr) {
                CommissionExModel *commissionExModel = [[CommissionExModel alloc] initWithDictionary:dict error:nil];
                
                if (commissionExModel) {
                    [self.commissiomExArray addObject:commissionExModel];
                }
                
            }
            
        }else{
            
            if (index == 1) {
                
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_NoRecord"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/1.5);
                    make.height.mas_equalTo(WIDTH/2.4);
                }];
                
                [self.tableView removeFromSuperview];
                self.view.backgroundColor = BACKGROUND_COLOR;

            }else{
                
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
        [_progressHUD hide:true];
        [self.tableView reloadData];
    }];
    
}

#pragma mark >>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.commissiomExArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommissionExCell *cell = [tableView dequeueReusableCellWithIdentifier:commissionExIdentifier ];
    
    if (cell == nil) {
        cell = [[CommissionExCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:commissionExIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    CommissionExModel *commissionExModel = self.commissiomExArray[indexPath.section];
//
    cell.userNameLabel.text = [NSString stringWithFormat:@"购买用户:%@",commissionExModel.username];
    
    if ([commissionExModel.first_level_user_incr length] > 0) {
        cell.extensionMoney.text = commissionExModel.first_level_user_incr;
        cell.extensionLevelImg.image = [UIImage imageNamed:@"icon_extenseLevel_1"];
    }else{
        cell.extensionMoney.text = commissionExModel.second_level_user_incr;
        cell.extensionLevelImg.image = [UIImage imageNamed:@"icon_extenseLevel_2"];
    }
    
    cell.goodsNameLabel.text = commissionExModel.product_name;
    cell.timeLabel.text = commissionExModel.create_time;
    cell.goodsNumLabel.text = [NSString stringWithFormat:@"x%@",commissionExModel.goods_num];
    cell.goodsPriceLabel.text = [NSString stringWithFormat:@"¥%@",commissionExModel.money];
    
//    //图片
    [cell.goodsImg sd_setImageWithURL:[NSURL URLWithString:commissionExModel.product_image] placeholderImage:[UIImage imageNamed:@"goodsLoadJGG"]];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSMutableArray *)commissiomExArray
{
    if (!_commissiomExArray) {
        _commissiomExArray = [NSMutableArray array];
    }
    return _commissiomExArray;
}

@end
